package org.csu.petserver.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.petserver.domain.Client;
import org.csu.petserver.domain.Item;
import org.csu.petserver.domain.LineItem;
import org.csu.petserver.domain.Order;
import org.csu.petserver.persistence.ItemMapper;
import org.csu.petserver.persistence.LineItemMapper;
import org.csu.petserver.persistence.OrderMapper;
import org.csu.petserver.service.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static java.lang.Integer.parseInt;

@Repository("OrderInfo")
@Service
public class OrderInfoImpl implements OrderInfo {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private LineItemMapper lineItemMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Override
    public List<LineItem> getOrderSpecificInfo(Order order) {
        QueryWrapper<LineItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderid",order.getId());
        List<LineItem> lineItems = lineItemMapper.selectList(queryWrapper);
        return lineItems;
    }

    @Override
    public List<Item> getAllItemNotInOrder(Order order) {
        QueryWrapper<LineItem> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("orderid",order.getId());
        List<LineItem> lineItems= lineItemMapper.selectList(queryWrapper);
        List<String> itemids = new ArrayList<>();
        for (LineItem lineItem:lineItems) {
            itemids.add(lineItem.getItemid());
        }
        QueryWrapper<Item> queryWrapper1 = new QueryWrapper<>();
        for (String itemid: itemids) {
            queryWrapper1.ne("itemid",itemid);
        }
        List<Item> items = itemMapper.selectList(queryWrapper1);
        return items;
    }


    @Override
    public boolean deleteOrder(Order order) {
        orderMapper.deleteById(order);
        QueryWrapper<LineItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderid",order.getId());
        lineItemMapper.delete(queryWrapper);
        return true;
    }

    @Override
    public List<Order> searchOrderOfAllClient(String keyword) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("userid",keyword);
        List<Order> orders = orderMapper.selectList(queryWrapper);
        return orders;
    }

    @Override
    public List<Order> searchExactOrderOfCertainClient(Client client, String keyword) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",client.getId())
                .eq("orderid",keyword);
        List<Order> orders = orderMapper.selectList(queryWrapper);
        return orders;
    }

    @Override
    public List<Order> sortByDate(List<Order> orders) {
        orders.sort((t1,t2)->t2.getOrderdate().compareTo(t1.getOrderdate()));
        return orders;
    }

    @Override
    public List<Order> sortByPrice(List<Order> orders) {
        orders.sort((t1,t2)->t2.getTotalprice().compareTo(t1.getTotalprice()));
        return orders;
    }

    @Override
    public boolean updateOrder(Order order) {
        orderMapper.updateById(order);
        return true;
    }

    @Override
    public boolean updateOrderItem(List<LineItem> lineItems, Order order){
        double totalPrice= 0.0;
        for (LineItem lineItem:lineItems) {
            lineItemMapper.updateById(lineItem);
            totalPrice += lineItem.getQuantity().doubleValue() * lineItem.getUnitprice().doubleValue();
        }
        order.setTotalprice(BigDecimal.valueOf(totalPrice));
        orderMapper.updateById(order);
        return true;
    }

    @Override
    public boolean shipping(Order order) {
        if(order.getStatus()==1){
            return false;
        }
        else{
            order.setStatus(1);//0代表未发货，1代表已发货
            orderMapper.updateById(order);
            return true;
        }

    }

    @Override
    public List<Order> selectOrderNotShipping(List<Order> orders) {
        List<Order> newOrders = new ArrayList<>();
        for (Order order: orders) {
            if(order.getStatus()==0){
                newOrders.add(order);
            }
        }
        return newOrders;
    }

    @Override
    public List<Order> selectOrderShipping(List<Order> orders) {
        List<Order> newOrders = new ArrayList<>();
        for (Order order: orders) {
            if(order.getStatus()==1){
                newOrders.add(order);
            }
        }
        return newOrders;
    }

    //获取所有订单
    @Override
    public List<Order> getAllOrder(){
        return orderMapper.selectList(null);
    }

    @Override
    public Order selectOrderById(String orderId) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        int orderid=parseInt(orderId);
        queryWrapper.eq("orderid",orderid);
        return orderMapper.selectOne(queryWrapper);
    }

    @Override
    public BigDecimal getTodayBanking(String date) throws ParseException {
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse(date);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderdate",date);
        List<Order> orders = orderMapper.selectList(queryWrapper);
        Double totalEarning = 0.0;
        for (Order order:orders) {
            totalEarning +=order.getTotalprice().doubleValue();
        }
        return BigDecimal.valueOf(totalEarning);
    }
    @Override

    public List<Order> getOrderByDate(String date) throws ParseException {
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse(date);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderdate",date);
        List<Order> orders = orderMapper.selectList(queryWrapper);
        return orders;
    }
}
